/* **********************************************************************************
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* **********************************************************************************/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OfficeOpenXMLInterop;

namespace Theme_Builder
{
    public partial class EditInnerShadow : Form
    {
        public EditInnerShadow()
        {
            InitializeComponent();
        }

        public OfficeOpenXMLInterop.CT_InnerShadowEffect InnerShadow
        {
            get
            {
                OfficeOpenXMLInterop.CT_InnerShadowEffect shadow = new OfficeOpenXMLInterop.CT_InnerShadowEffect();

                shadow.srgbClr = new CT_SRgbColor();
                shadow.srgbClr.val = Converters.ColorToByteRBG(this.panelColor.BackColor);

                int fooInt = Int32.Parse(this.textBoxAlpha.Text);
                if (fooInt != 100)
                {
                    shadow.srgbClr.alpha = new CT_PositiveFixedPercentage();
                    shadow.srgbClr.alpha.val = fooInt * 1000;
                }

                shadow.blurRad = (long)(float.Parse(this.textBoxBlurRad.Text) * 12700f);
                shadow.dist = (long)(float.Parse(this.textBoxDist.Text) * 12700f);
                shadow.dir = (int)(float.Parse(this.textBoxDir.Text) * 60000f);

                return shadow;
            }
            set
            {
                if (value.srgbClr != null)
                {
                    this.panelColor.BackColor = Converters.ByteRGBToColor(value.srgbClr.val);

                    if (value.srgbClr.alpha != null)
                    {
                        this.textBoxAlpha.Text = ((float)(value.srgbClr.alpha.val) / 1000f).ToString();
                    }
                }

                this.textBoxBlurRad.Text = ((float)(value.blurRad) / 12700f).ToString();
                this.textBoxDist.Text = ((float)(value.dist) / 12700f).ToString();
                this.textBoxDir.Text = ((float)(value.dir) / 60000f).ToString();
            }
        }

        private void panelColor_MouseClick(object sender, MouseEventArgs e)
        {
            this.colorDialog1.Color = this.panelColor.BackColor;

            if (this.colorDialog1.ShowDialog(this) == DialogResult.OK)
            {
                this.panelColor.BackColor = this.colorDialog1.Color;

                // Invalidate the panel so that it repaints
                this.panelColor.Invalidate();
            }
        }

        private void panelColor_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = Graphics.FromHwnd(this.panelColor.Handle);

            RectangleF bounds = g.VisibleClipBounds;

            ColorMods cMods = new ColorMods();
            cMods.alpha = Double.Parse(this.textBoxAlpha.Text);

            Color color = Converters.ModifyColor(this.panelColor.BackColor, cMods);

            SolidBrush sb = new SolidBrush(color);

            g.FillRectangle(sb, bounds);

            g.Dispose();
        }

        private void textBoxAlpha_Validated(object sender, EventArgs e)
        {
            this.panelColor.Invalidate();
        }
    }
}